// ==UserScript==
// @name         Fullscreen Button
// @name:zh-CN   全屏 - Ctrl+点击
// @match        *://*/*
// @version      0.2
// @description  A simple float button for entering fullscreen.
// @description:zh-CN 一个简单的浮动按钮用于全屏显示网页。
// @noframes
// @license      CC0
// @namespace    https://greasyfork.org/users/123506
// @downloadURL https://update.greasyfork.org/scripts/474531/Fullscreen%20Button.user.js
// @updateURL https://update.greasyfork.org/scripts/474531/Fullscreen%20Button.meta.js
// ==/UserScript==

'use strict'
var DOC_EL = document.documentElement
var headEl = DOC_EL.querySelector('head')
var styleEl = document.createElement('style')
var TYPE_REQUEST_FULL_SCREEN = 'requestFullscreen'
var TYPE_EXIT_FULL_SCREEN = 'exitFullscreen'
var TYPE_FULL_SCREEN_ELEMENT = 'fullscreenElement'
var TYPE_ON_FULL_SCREEN_CHANGE = 'onfullscreenchange'
if ('webkitRequestFullScreen' in DOC_EL) {
  TYPE_REQUEST_FULL_SCREEN = 'webkitRequestFullScreen'
  TYPE_EXIT_FULL_SCREEN = 'webkitExitFullscreen'
  TYPE_FULL_SCREEN_ELEMENT = 'webkitFullscreenElement'
  TYPE_ON_FULL_SCREEN_CHANGE = 'onwebkitfullscreenchange'
} else if ('msRequestFullscreen' in DOC_EL) {
  TYPE_REQUEST_FULL_SCREEN = 'msRequestFullscreen'
  TYPE_EXIT_FULL_SCREEN = 'msExitFullscreen'
  TYPE_FULL_SCREEN_ELEMENT = 'msFullscreenElement'
  TYPE_ON_FULL_SCREEN_CHANGE = 'MSFullscreenChange'
} else if ('mozRequestFullScreen' in DOC_EL) {
  TYPE_REQUEST_FULL_SCREEN = 'mozRequestFullScreen'
  TYPE_EXIT_FULL_SCREEN = 'mozCancelFullScreen'
  TYPE_FULL_SCREEN_ELEMENT = 'mozFullScreenElement'
  TYPE_ON_FULL_SCREEN_CHANGE = 'onmozfullscreenchange'
} else if (!('requestFullscreen' in DOC_EL)) {
  throw '\u5F53\u524D\u6D4F\u89C8\u5668\u4E0D\u652F\u6301Fullscreen API !'
}
function getCurrentElement(el) {
  return el instanceof HTMLElement ? el : DOC_EL
}
function beFull(el, backgroundColor) {
  if (backgroundColor) {
    if (null === headEl) {
      headEl = document.createElement('head')
    }
    styleEl.innerHTML = ':fullscreen{background-color:' + backgroundColor + ';padding: 30px;overflow:auto;}'
    setTimeout(() => {
      headEl.appendChild(styleEl)
    }, 10)
  }
  return getCurrentElement(el)[TYPE_REQUEST_FULL_SCREEN]()
}
function exitFull() {
  if (DOC_EL.contains(styleEl)) {
    headEl === null || headEl === void 0 ? void 0 : headEl.removeChild(styleEl)
  }
  return document[TYPE_EXIT_FULL_SCREEN]()
}
function isFull(el) {
  return getCurrentElement(el) === document[TYPE_FULL_SCREEN_ELEMENT]
}
function toggleFull(el, backgroundColor) {
  if (isFull(el)) {
    exitFull()
    return false
  } else {
    beFull(el, backgroundColor)
    return true
  }
}

setTimeout(() => {
  const HeightLimit = 200
  function findMeaningfulElement(target) {
    let res
    if (target.getBoundingClientRect().height < HeightLimit) {
      res = findMeaningfulElement(target.parentElement)
    } else {
      res = target
    }

    return res
  }

  document.body.addEventListener('click', function (e) {
    if (!e.ctrlKey) {
      return
    }

    const targetElement = findMeaningfulElement(event.target)
    beFull(targetElement, '#fff')
  })
}, 1000)
